package com.sundi.ecgeneralize.web;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sundi.ecgeneralize.dto.AdDto;
import com.sundi.ecgeneralize.dto.CouponDto;
import com.sundi.ecgeneralize.dto.ResultEntity;
import com.sundi.ecgeneralize.entity.EcAd;
import com.sundi.ecgeneralize.entity.EcCoupon;
import com.sundi.ecgeneralize.service.IEcAdService;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.io.ByteArrayOutputStream;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 广告表 前端控制器
 * </p>
 *
 * @author Mht
 * @since 2020-03-12
 */
@RestController
@CrossOrigin
@RequestMapping("/ecAd")
public class EcAdController {

    @Autowired
    IEcAdService ecAdService;

    @RequestMapping(value = "adList", method = RequestMethod.GET)
    public ResultEntity adList(AdDto adDto){
        PageHelper.startPage(adDto.getPageNum(),adDto.getPageSize());
        List<EcAd> ecAds = ecAdService.selectAdList(adDto);
        PageInfo<EcAd> info = new PageInfo<>(ecAds);
        return ResultEntity.ok(info);
    }

    @RequestMapping("/addAd")
    public ResultEntity addAd(@RequestBody AdDto adDto){
        EcAd ecAd = new EcAd();
        BeanUtils.copyProperties(adDto, ecAd);
        if (ecAd.getId() == null) {
            ecAd.setAddTime(new Date());
        }
        boolean insert = ecAdService.insert(ecAd);
        return ResultEntity.ok(insert);
    }

    //    删除
    @RequestMapping(value = "/delAd")
    public ResultEntity del(Integer id) {
        System.err.println(id);
        ecAdService.updAndDel(id);
        return ResultEntity.ok();
    }


    //回显
    @RequestMapping(value = "/getAd")
    public ResultEntity getAd(Integer id) {
        EcAd ad = ecAdService.getAd(id);
        System.err.println(ad);
        return ResultEntity.ok(ad);
    }
    //    修改
    @RequestMapping(value = "/updAd")
    public ResultEntity updAd(@RequestBody AdDto adDto) {
        System.out.println(adDto);
        EcAd ecAd = new EcAd();
        BeanUtils.copyProperties(adDto, ecAd);
        if (ecAd.getId() != null) {
            ecAd.setUpdateTime(new Date());
        }
        boolean b = ecAdService.updateById(ecAd);
        return ResultEntity.ok(b);
    }


    @RequestMapping("/downLoadAdExcel")
    public ResponseEntity downLoadAdExcel(AdDto adDto) throws Exception{
        XSSFWorkbook xssfSheets = ecAdService.downLoadAdExcel(adDto);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        xssfSheets.write(byteArrayOutputStream);

        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentDispositionFormData("attachment",new String("广告管理列表.xlsx".getBytes("UTF-8"), "ISO-8859-1"));
        httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity(byteArrayOutputStream.toByteArray(),httpHeaders, HttpStatus.CREATED);
    }

}
